﻿using System;
using System.Net;
using System.Runtime.Serialization.Json;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;

namespace WellDone
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    /// 

    public sealed partial class PartialPaymentDialog : Page
    {
        public event EventHandler CloseRequested;
        public Grid innerDialog;
        public string checkNumber;
        CloseOrder co = new CloseOrder();
        public decimal dPartial = 0;
        Popup dialogPopup = new Popup();

        public PartialPaymentDialog()
        {
            this.InitializeComponent();
            innerDialog = LayoutRoot;
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            if (this.CloseRequested != null)
            {
                this.CloseRequested(this, null);
            }
        }

        public void Init(decimal PartialAmount)
        {
            try
            {
                txtAmount.Text = PartialAmount.ToString("N");
                dPartial = PartialAmount;
                if (Globals.processBlackline)
                {
                    txtAuthCode.IsEnabled = false;
                }
            }
            catch { }
        }

        private async void btnOk_Click(object sender, RoutedEventArgs e)
        {
            bool bResult = false;
            try
            {
                if (txtAmount.Text.Trim() != "")
                {
                    if (Convert.ToDecimal(txtAmount.Text) > 0)
                    {
                        CloseOrder.partialCashAmount = Convert.ToDecimal(txtAmount.Text);
                        CloseOrder.partialCreditAmount = Convert.ToDecimal(txtCreditAmount.Text);
                        CloseOrder.partialCheckNo = Convert.ToInt32(checkNumber); // Globals.OrderCheckRecords[Convert.ToInt32(checkNumber)].col_ID;
                        // Check if processing credit using Blackline...
                        if (Globals.processBlackline)
                        {
                            // Display the blackline dialog..
                            BlacklineDialog bd = new BlacklineDialog();
                            bd.Width = Windows.UI.Xaml.Window.Current.Bounds.Width;
                            bd.Height = Windows.UI.Xaml.Window.Current.Bounds.Height;
                            bd.CloseRequested += BlacklineDialog_CloseRequested;
                            dialogPopup.Child = bd;
                            dialogPopup.IsOpen = true;
                            bResult = await bd.processSale(CloseOrder.partialCreditAmount, Globals.OrderCheckRecords[CloseOrder.partialCheckNo].col_ID);
                            txtAuthCode.Text = bd.strAuthCode;
                        }
                        if (txtAuthCode.Text.Trim() != "")
                        {
                            CloseOrder.AuthCode = txtAuthCode.Text;
                        }
                    }
                }
            }
            catch { }
            if (bResult == false)
                return;
            if (this.CloseRequested != null)
            {
                this.CloseRequested(this, EventArgs.Empty);
            }
        }

        private void BlacklineDialog_CloseRequested(object sender, EventArgs e)
        {
            BlacklineDialog bd = (BlacklineDialog)sender;
            dialogPopup.IsOpen = false;
        }

        private void txtAmount_TextChanged(object sender, TextChangedEventArgs e)
        {
            try
            {
                decimal CreditAmoumt = 0;
                if (txtAmount.Text != "")
                {
                    CreditAmoumt = dPartial - Convert.ToDecimal(txtAmount.Text);
                    txtCreditAmount.Text = CreditAmoumt.ToString("N");
                }
            }
            catch { }
        }
    }
}
